package com.dayuanit.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.dayuanit.controller.request.AddEquipmentParam;
import com.dayuanit.controller.request.EditEquipmentParam;
import com.dayuanit.entity.Equipment;
import com.dayuanit.response.ExResponse;
import com.dayuanit.service.EquipmentService;
import com.dayuanit.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备接口
 */
@RestController
@RequestMapping(value = "equipment")
public class EquipmentController {


    @Autowired
    private EquipmentService equipmentService;

    /**
     * 设备列表 分页查询
     * @param equipmentName
     * @param equipmentId
     * @return
     */
    @GetMapping(value = "queryEquipmentList")
    public ExResponse queryEquipmentList(@RequestParam(value = "equipmentName", required = false) String equipmentName,
                                         @RequestParam(value = "equipmentId", required = false) Long equipmentId,
                                         @RequestParam(value = "page") Integer page,
                                         @RequestParam(value = "limit") Integer pageSize) {

        PageInfo pageInfo = equipmentService.queryEquipmentListByIdOrName(equipmentId, equipmentName, page, pageSize);

        return ExResponse.buildSuccessList(pageInfo.getData(), pageInfo.getTotalCount());
    }


    /**
     * 新增设备
     * @return
     */
    @PostMapping(value = "addEquipment")
    public ExResponse addEquipment(@RequestBody AddEquipmentParam addEquipmentParam) {
        equipmentService.addEquipment(addEquipmentParam);
        return ExResponse.buildSuccessWithoutInfo();
    }


    /**
     * 批量删除设备
     * @param idList
     * @return
     */
    @PostMapping(value = "deleteEquipment")
    public ExResponse deleteEquipment(@RequestBody List<Long> idList) {
        if(idList.size() == 0) {
            return ExResponse.buildSuccessWithoutInfo();
        }else {
            equipmentService.batchDelete(idList);

            return ExResponse.buildSuccessWithoutInfo();
        }
    }

    /**
     * 编辑设备基础信息
     * @param param
     * @return
     */
    @PostMapping(value = "editEquipment")
    public ExResponse editEquipment(@RequestBody EditEquipmentParam param) {
        equipmentService.editEquipment(param);
        return ExResponse.buildSuccessWithoutInfo();
    }

    /**
     * 下拉框使用的设备列表信息
     * @return
     */
    @GetMapping(value = "queryEquipment")
    public ExResponse queryEquipment() {
        List<Equipment> equipmentList = equipmentService.queryEquipment();
        return ExResponse.buildSuccessList(equipmentList,(long)equipmentList.size());
    }
}
